﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Security.Server;
namespace LightSwitchApplication
{
	public partial class ApplicationDataService
	{
		partial void Languages_Validate(Language entity, EntitySetValidationResultsBuilder results)
		{
			try
			{
				var culture = new System.Globalization.CultureInfo(entity.Name);

				var parentCulture = culture.Parent;
				bool mismatch = false;

				if (parentCulture.Equals(System.Globalization.CultureInfo.InvariantCulture))
				{
					if (entity.Parent != null)
					{
						mismatch = true;
					}
				}
				else
				{
					if (entity.Parent == null)
					{
						mismatch = true;
					}
					else if (!string.Equals(entity.Parent.Name, parentCulture.Name, StringComparison.Ordinal))
					{
						mismatch = true;
					}
				}

				if (mismatch)
				{
					results.AddEntityError("There is a mismatch between child culture and parent culture.");
				}
			}
			catch (System.Globalization.CultureNotFoundException ex)
			{
				results.AddEntityError(ex.Message);
			}
		}
	}
}
